package com.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * 自定义一个过滤关键字的拦截器
 */
public class KeyWordsInterceptor implements HandlerInterceptor {

    /**
     * 在处理器执行之前执行的方法
     * @param request
     * @param response
     * @param handler
     * @return boolean类型表示为当前的请求是否允许被放行
     *              true 可以继续向下执行 处理器的逻辑
     *              false 中断了后续的操作 止步于此
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        boolean boo = true;


        List<String> blackList = new ArrayList<>();
        blackList.add("法轮功");
        blackList.add("64运动");

        //在处理器之前获取页面的数据
        String content = request.getParameter("content");

        //获取到原始数据后直接存放到request中
        request.setAttribute("beforeComment",content);

        //如果评论内容有以下关键字，性质比较恶劣，直接拦截不放行,转发到原评论页面
        if(null!=content && content.contains("退党保全家")){
            System.out.println("太缺德");
            boo = false;
//            request.setAttribute("info","太缺德");
//            request.getRequestDispatcher("/jsp/comment.jsp").forward(request,response);
        }else if (null!=content){
            //如果评论内容的关键字没有那么恶劣的话，经过过滤可以正常放行
            //遍历黑名单中所有的关键词，进行过滤
            for (String black : blackList) {
                if(content.contains(black)){
                    content = content.replaceAll(black,"*****");
                }
            }

            //把过滤之后的评论内容也发送到request中
            request.setAttribute("afterComment",content);
        }

        return boo;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

        System.out.println("beforeComment:"+request.getAttribute("beforeComment"));
        System.out.println("afterComment:"+request.getAttribute("afterComment"));

        System.out.println("===========================================");

        String afterComment = (String) request.getAttribute("afterComment");

        if(null!=afterComment){
            afterComment = afterComment.replaceAll("\\*\\*\\*\\*\\*","^-^*****^_^");
        }

        request.setAttribute("afterComment",afterComment);

    }

    /**
     * 在页面渲染完毕之后执行的方法
     * 一般做一些资源清理或日志记录之类的工作
     * 不能再更新数据了
     * @param request
     * @param response
     * @param handler
     * @param ex
     * @throws Exception
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        //生成日志的日期时间
        String logDateTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        String commentContent = (String) request.getAttribute("beforeComment");
        String filterCommentContent = (String) request.getAttribute("afterComment");

        System.out.println(logDateTime+"拦截了一条数据："+commentContent+";过滤为:"+filterCommentContent);

    }
}
